package com.inspireink.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inspireink.project.entity.InspireUser;
import com.inspireink.project.mapper.InspireUserMapper;
import com.inspireink.project.service.InspireUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


@Service
public class InspireUserServiceImpl implements InspireUserService  {
    @Autowired
    private InspireUserMapper inspireUserMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void register(InspireUser user)  {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        inspireUserMapper.insert(user);
    }

    @Override
    public InspireUser login(String username, String password) {
        QueryWrapper<InspireUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        InspireUser user = inspireUserMapper.selectOne(queryWrapper);
        if (user != null && passwordEncoder.matches(password, user.getPassword())) {
            return user;
        }
        return null;
    }
}
